package com.sbl.bajw.pc.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.AdvertisingModel;
import com.sbl.bajw.pc.service.AdvertisingModelService;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/Advertising")
public class AdvertisingModelAction {

	@Resource
	private AdvertisingModelService service;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("Advertising");
	}
	
	@RequestMapping(value = "/create")
	public @ResponseBody String create(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		AdvertisingModel model = RequestUnit.getModel(AdvertisingModel.class);
		model.setCreatedBy(SessionUnit.getUserId());
		service.insert(model);
		String json = JsonResultUtil.Success();
		return json;
	}
	
	@RequestMapping(value = "/getAdvertisingList")
	public @ResponseBody String getAdvertisingList(HttpServletRequest request, HttpServletResponse response){
		String json = service.getAdvertisingList();
		return json;
	}
	
	@RequestMapping(value = "/getAdvertisingApp")
	public @ResponseBody String getAdvertisingApp(HttpServletRequest request, HttpServletResponse response){
		log.info("获取广告图片");
		List<HashMap<String, Object>> list = service.getAdvertisingApp();
		log.info("广告图片：" + list);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(HttpServletRequest request, HttpServletResponse response){
		String ids = RequestUnit.getString("ids");
		for (String id : ids.split(",")) {
			AdvertisingModel model = new AdvertisingModel();
			model.setId(id);
			model.setStatus("0");
			model.setUpdateBy(SessionUnit.getUserId());
			service.update(model);
		}
		String json = JsonResultUtil.Success();
		return json;
	}
}
